// https://leetcode.cn/problems/one-away-lcci/

// 字符串有三种编辑操作:插入一个字符、删除一个字符或者替换一个字符。 给定两个字符串，编写一个函数判定它们是否只需要一次(或者零次)编辑。

#include <string>
#include <iostream>

using namespace std;


class Solution {
public:
    bool oneEditAway(string first, string second) {
		int len1 = first.size();
		int len2 = second.size();
		if(abs(len1 - len2) > 1){
			return false;
		}
		if(len1 == 0 || len2 == 0){
			return true;
		}
		
		int p1 = 0, p2 = 0, diff = 0;
		while(p1 < len1 && p2 < len2){
			if(first[p1] == second[p2]){
				p1++;
				p2++;
			}else{
				diff++;
				if(first[p1+1] == second[p2]){
					p1 += 2;
					p2++;
				} else if(first[p1] == second[p2+1]){
					p1++;
					p2 += 2;
				}else{
					p1++;
					p2++;
				}
				if(diff > 1){
					return false;
				}
			}
		}
		
		if((p1 != len1 || p2 != len2) && diff == 1){
			return false;
		}
		return true;
    }
};

int main(){
	Solution so;
	string a = "a";
	string b = "b";
	if(so.oneEditAway(a, b)){
		cout << "ok" << endl;
	}else{
		cout << "err" << endl;
	}
	return 0;
}